<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入客户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="当前状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option label="正常" value="normal" />
          <el-option label="停机" value="stopped" />
          <el-option label="预约停机" value="scheduled" />
        </el-select>
      </el-form-item>
      <el-form-item label="停机原因" prop="reason">
        <el-select v-model="queryParams.reason" placeholder="请选择停机原因" clearable>
          <el-option label="欠费停机" value="arrears" />
          <el-option label="主动停机" value="voluntary" />
          <el-option label="违规停机" value="violation" />
          <el-option label="其他原因" value="other" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-video-pause"
          size="mini"
          @click="handleStop"
          v-hasPermi="['system:account:stop']"
        >停机</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-video-play"
          size="mini"
          @click="handleResume"
          v-hasPermi="['system:account:resume']"
        >复机</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-time"
          size="mini"
          @click="handleSchedule"
          v-hasPermi="['system:account:schedule']"
        >预约停复机</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stopOnList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户姓名" align="center" prop="name" />
      <el-table-column label="手机号码" align="center" prop="phoneNumber" />
      <el-table-column label="当前状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 'normal' ? 'success' : scope.row.status === 'stopped' ? 'danger' : 'warning'">
            {{ scope.row.status === 'normal' ? '正常' : scope.row.status === 'stopped' ? '停机' : '预约停机' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="停机原因" align="center" prop="reason" />
      <el-table-column label="停机时间" align="center" prop="stopTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.stopTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计复机时间" align="center" prop="resumeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.resumeTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleViewHistory(scope.row)"
          >查看历史</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 停机操作对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="停机原因" prop="reason">
          <el-select v-model="form.reason" placeholder="请选择停机原因">
            <el-option label="欠费停机" value="arrears" />
            <el-option label="主动停机" value="voluntary" />
            <el-option label="违规停机" value="violation" />
            <el-option label="其他原因" value="other" />
          </el-select>
        </el-form-item>
        <el-form-item label="预计复机" prop="resumeTime">
          <el-date-picker
            v-model="form.resumeTime"
            type="datetime"
            placeholder="请选择预计复机时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入备注"
            maxlength="200"
            show-word-limit
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 历史记录抽屉 -->
    <el-drawer
      title="停复机历史记录"
      :visible.sync="historyDrawer"
      direction="rtl"
      size="50%">
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in historyList"
          :key="index"
          :type="activity.type === 'stop' ? 'danger' : 'success'"
          :timestamp="activity.time">
          {{activity.type === 'stop' ? '停机' : '复机'}} - {{activity.reason}}
          <div class="history-content">
            <p>操作人: {{activity.operator}}</p>
            <p>备注: {{activity.remark}}</p>
          </div>
        </el-timeline-item>
      </el-timeline>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: "StopOn",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 停开机表格数据
      stopOnList: [
        {
          name: '张三',
          phoneNumber: '13800138000',
          status: 'normal',
          reason: '',
          stopTime: null,
          resumeTime: null,
          remark: ''
        },
        {
          name: '李四',
          phoneNumber: '13800138001',
          status: 'stopped',
          reason: '欠费停机',
          stopTime: '2023-10-01 10:00:00',
          resumeTime: '2023-10-10 10:00:00',
          remark: '欠费超过30天'
        },
        {
          name: '王五',
          phoneNumber: '13800138002',
          status: 'scheduled',
          reason: '主动停机',
          stopTime: '2023-10-15 10:00:00',
          resumeTime: '2023-10-20 10:00:00',
          remark: '用户申请'
        }
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示历史记录抽屉
      historyDrawer: false,
      // 历史记录列表
      historyList: [
        {
          type: 'stop',
          reason: '欠费停机',
          time: '2023-09-01 10:00:00',
          operator: '管理员',
          remark: '欠费超过30天'
        },
        {
          type: 'resume',
          reason: '缴费复机',
          time: '2023-09-10 15:00:00',
          operator: '管理员',
          remark: '已缴清欠费'
        }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        phoneNumber: null,
        status: null,
        reason: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        reason: [
          { required: true, message: "停机原因不能为空", trigger: "change" }
        ],
        resumeTime: [
          { required: true, message: "预计复机时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询停开机列表 */
    getList() {
      this.loading = true;
      setTimeout(() => {
        this.loading = false;
      }, 500);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        reason: null,
        resumeTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.phoneNumber)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 停机操作 */
    handleStop() {
      this.reset();
      this.open = true;
      this.title = "停机操作";
    },
    /** 复机操作 */
    handleResume() {
      this.$modal.msgSuccess("复机操作成功");
      this.getList();
    },
    /** 预约停复机操作 */
    handleSchedule() {
      this.$modal.msgSuccess("预约停复机操作成功");
      this.getList();
    },
    /** 查看历史记录 */
    handleViewHistory(row) {
      this.historyDrawer = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.$modal.msgSuccess("操作成功");
          this.open = false;
          this.getList();
        }
      });
    }
  }
};
</script>

<style scoped>
.history-content {
  padding: 10px;
  background: #f5f7fa;
  border-radius: 4px;
  margin: 10px 0;
}

.history-content p {
  margin: 5px 0;
  color: #606266;
  font-size: 14px;
}
</style>